package com.fun_ordering.controller;

import com.fun_ordering.components.ApiResponse;
import com.fun_ordering.components.CodeComponents;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.pojo.DishInfo;
import com.fun_ordering.pojo.DishRatio;
import com.fun_ordering.pojo.MaterialStock;
import com.fun_ordering.service.DishInfoService;
import com.fun_ordering.service.DishRatioService;
import com.fun_ordering.service.MaterialStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 菜品信息表controller
 * @author XiaoPi
 *
 */
@RestController
@RequestMapping("/dishInfo")
public class DishInfoController {

	@Autowired
	private DishInfoService dishInfoService;

	@Autowired
	private DishRatioService dishRatioService;

	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<DishInfo> findAll(){
		return dishInfoService.findAll();
	}


	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult  findPage(int page,int rows){
		return dishInfoService.findPage(page, rows);
	}

	/**
	 * 增加
	 * @param dishInfo
	 * @return
	 */
	@RequestMapping("/add")
	public ApiResponse add(@RequestBody DishInfo dishInfo){
		try {
			dishInfo.setIsDelete(CodeComponents.DELETE_NO);
			dishInfoService.add(dishInfo);
			for (DishRatio dishRatio : dishInfo.getDishRatioList()) {
				dishRatio.setDishId(dishInfo.getDishId());
				dishRatioService.add(dishRatio);
			}
		return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
		return ApiResponse.failed(null);
		}
	}

	/**
	 * 修改
	 * @param dishInfo
	 * @return
	 */
	@RequestMapping("/update")
	@Transactional
	public ApiResponse update(@RequestBody DishInfo dishInfo){
		try {
			dishRatioService.deleteByDishId(dishInfo.getDishId());
			dishInfoService.update(dishInfo);
			for (DishRatio dishRatio : dishInfo.getDishRatioList()) {
				dishRatio.setDishId(dishInfo.getDishId());
				dishRatioService.add(dishRatio);
			}
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

	/**
	 * 获取实体
	 * @param dishId
	 * @return
	 */
	@RequestMapping("/findOne")
	public DishInfo findOne(Integer dishId){
		return dishInfoService.findOne(dishId);
	}

	/**
	 * 批量删除
	 * @param dishIds
	 * @return
	 */
	@RequestMapping("/delete")
	@Transactional
	public ApiResponse delete(Integer [] dishIds){
		try {
			for (Integer dishId: dishIds ) {
				dishRatioService.deleteByDishId(dishId);
			}
			dishInfoService.delete(dishIds);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

		/**
	 * 查询+分页
	 * @param dishInfo
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/pageSearch")
	public PageResult search(@RequestBody DishInfo dishInfo, int page, int rows  ){
//		System.out.println("dishInfo = " + dishInfo);
		return dishInfoService.findPage(dishInfo, page, rows);
	}
}
